/*-------------------------------------------------------------------------
 *
 *	  ISO8859_1 <--> UTF8
 *
 * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 * IDENTIFICATION
 *	  src/backend/utils/mb/conversion_procs/utf8_and_iso8859_1/utf8_and_iso8859_1.c
 *
 *-------------------------------------------------------------------------
 */

#include "postgres.h"
#include "fmgr.h"
#include "mb/pg_wchar.h"

PG_MODULE_MAGIC;

PG_FUNCTION_INFO_V1(iso8859_1_to_utf8);
PG_FUNCTION_INFO_V1(utf8_to_iso8859_1);

/* ----------
 * conv_proc(
 *		INTEGER,	-- 源编码id
 *		INTEGER,	-- 目标编码id
 *		CSTRING,	-- 源字符串（以null结尾的C字符串）
 *		CSTRING,	-- 目标字符串（以null结尾的C字符串）
 *		INTEGER,	-- 源字符串长度
 *		BOOL		-- 如果为真，转换失败时不抛出错误
 * ) 返回INTEGER;
 *
 * 返回成功转换的字节数。
 * ----------
 */

Datum iso8859_1_to_utf8(PG_FUNCTION_ARGS)
{
	unsigned char *fc_src = (unsigned char *) PG_GETARG_CSTRING(2);
	unsigned char *fc_dest = (unsigned char *) PG_GETARG_CSTRING(3);
	int			fc_len = PG_GETARG_INT32(4);
	bool		fc_noError = PG_GETARG_BOOL(5);
	unsigned char *fc_start = fc_src;
	unsigned short fc_c;

	CHECK_ENCODING_CONVERSION_ARGS(PG_LATIN1, PG_UTF8);

	while (fc_len > 0)
	{
		fc_c = *fc_src;
		if (fc_c == 0)
		{
			if (fc_noError)
				break;
			report_invalid_encoding(PG_LATIN1, (const char *) fc_src, fc_len);
		}
		if (!IS_HIGHBIT_SET(fc_c))
			*fc_dest++ = fc_c;
		else
		{
			*fc_dest++ = (fc_c >> 6) | 0xc0;
			*fc_dest++ = (fc_c & 0x003f) | HIGHBIT;
		}
		fc_src++;
		fc_len--;
	}
	*fc_dest = '\0';

	PG_RETURN_INT32(fc_src - fc_start);
}

Datum utf8_to_iso8859_1(PG_FUNCTION_ARGS)
{
	unsigned char *fc_src = (unsigned char *) PG_GETARG_CSTRING(2);
	unsigned char *fc_dest = (unsigned char *) PG_GETARG_CSTRING(3);
	int			fc_len = PG_GETARG_INT32(4);
	bool		fc_noError = PG_GETARG_BOOL(5);
	unsigned char *fc_start = fc_src;
	unsigned short fc_c,
				fc_c1;

	CHECK_ENCODING_CONVERSION_ARGS(PG_UTF8, PG_LATIN1);

	while (fc_len > 0)
	{
		fc_c = *fc_src;
		if (fc_c == 0)
		{
			if (fc_noError)
				break;
			report_invalid_encoding(PG_UTF8, (const char *) fc_src, fc_len);
		}
		/* 针对 ASCII 子集字符的快速路径 */
		if (!IS_HIGHBIT_SET(fc_c))
		{
			*fc_dest++ = fc_c;
			fc_src++;
			fc_len--;
		}
		else
		{
			int			fc_l = pg_utf_mblen(fc_src);

			if (fc_l > fc_len || !pg_utf8_islegal(fc_src, fc_l))
			{
				if (fc_noError)
					break;
				report_invalid_encoding(PG_UTF8, (const char *) fc_src, fc_len);
			}
			if (fc_l != 2)
			{
				if (fc_noError)
					break;
				report_untranslatable_char(PG_UTF8, PG_LATIN1,
										   (const char *) fc_src, fc_len);
			}
			fc_c1 = fc_src[1] & 0x3f;
			fc_c = ((fc_c & 0x1f) << 6) | fc_c1;
			if (fc_c >= 0x80 && fc_c <= 0xff)
			{
				*fc_dest++ = (unsigned char) fc_c;
				fc_src += 2;
				fc_len -= 2;
			}
			else
			{
				if (fc_noError)
					break;
				report_untranslatable_char(PG_UTF8, PG_LATIN1,
										   (const char *) fc_src, fc_len);
			}
		}
	}
	*fc_dest = '\0';

	PG_RETURN_INT32(fc_src - fc_start);
}
